package com.czb.servlet;

import com.czb.service.RecordServcieImpl;
import com.czb.service.RecordService;
import com.czb.util.PageBean;
import com.czb.util.ResultJson;
import com.czb.util.Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/RecordServlet")
public class RecordServlet extends HttpServlet {

    RecordService recordService = new RecordServcieImpl();
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String action = req.getParameter("action");
        if(action.equals("findAll")){
            findAll(req,resp);
        }else if(action.equals("add")){
            add(req,resp);
        }else if(action.equals("update")){
            update(req,resp);
        }else if(action.equals("delete")){
            delete(req,resp);
        }else if(action.equals("findById")){
            findById(req,resp);
        }else if(action.equals("toAdd")){
            toAdd(req,resp);
        }else if(action.equals("toAddContr")){
            toAddContr(req,resp);
        }
    }

    private void toAddContr(HttpServletRequest req, HttpServletResponse resp) {
        String  sql = "select h_id,h_area,h_unitNumber,h_roomNo from house";
        List<Map> house = recordService.toAddHouse(sql);
        System.out.println(house);
        ResultJson success = ResultJson.success(house);
        Utils.returnJson(success,resp);
    }

    private void toAdd(HttpServletRequest req, HttpServletResponse resp) {
        String sql = "select * from logistics";
        List<Map> list = recordService.toAdd(sql);
        ResultJson success = ResultJson.success(list);
        Utils.returnJson(success,resp);
    }

    private void findById(HttpServletRequest req, HttpServletResponse resp) {
        Integer reid = Integer.valueOf(req.getParameter("reid"));
        String sql = "select r.*,l.loname,h.h_area,h.h_unitNumber,h.h_roomNo from record r,logistics l,house h where r.loid = l.loid and r.hid = h.h_id and r.reid = ?";
        Map map =  recordService.findById(sql,reid);
        ResultJson success = ResultJson.success(map);
        Utils.returnJson(success,resp);
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) {
        Integer reid = Integer.valueOf(req.getParameter("reid"));
        String sql = "delete from record where reid = ?";
        int i = recordService.delete(sql,reid);
        ResultJson success = ResultJson.success(i);
        Utils.returnJson(success,resp);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        Integer reid = Integer.valueOf(req.getParameter("reid"));
        Integer hid = Integer.valueOf(req.getParameter("h_id"));
        Integer loid = Integer.valueOf(req.getParameter("loid"));
        String retime = req.getParameter("retime");
        String reremark = req.getParameter("reremark");
        Object[] obj = {hid,loid,retime,reremark,reid};
        String sql = "update record set hid = ?,loid = ?,retime = ?,reremark = ? where reid = ? ";
        int i =  recordService.update(sql,obj);
        ResultJson success = ResultJson.success(i);
        Utils.returnJson(success,resp);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        Integer hid = Integer.valueOf(req.getParameter("h_id"));
        Integer loid = Integer.valueOf(req.getParameter("loid"));
        String retime = req.getParameter("retime");
        String reremark = req.getParameter("reremark");
        Object[] obj = {hid,loid,retime,reremark};
        String sql = "insert into record values(null,?,?,?,?)";
       int i =  recordService.add(sql,obj);
        ResultJson success = ResultJson.success(i);
        Utils.returnJson(success,resp);
    }

    private void findAll(HttpServletRequest req, HttpServletResponse resp) {
        Integer page = req.getParameter("page") != null ? Integer.valueOf(req.getParameter("page")) : 1;
        Integer limit = req.getParameter("limit") != null ? Integer.valueOf(req.getParameter("limit")) : 10;
        String  h_area =req.getParameter("h_area")!=null?"%"+req.getParameter("h_area")+"%":"%%";
        String  loname =req.getParameter("loname")!=null?"%"+req.getParameter("loname")+"%":"%%";
        System.out.println(page+limit+h_area+loname);
        String sql = "select r.*,l.loname,h.h_area,h.h_unitNumber,h.h_roomNo from record r,logistics l,house h where r.loid = l.loid and r.hid = h.h_id and h.h_area like ? and l.loname like ? ";
        PageBean<Map> pageBean = recordService.findAll(page,limit,sql,h_area,loname);
        System.out.println(pageBean);
        ResultJson success = ResultJson.success(pageBean);
        Utils.returnJson(success,resp);

    }
}
